#include "widget.h"
#include "ui_widget.h"

#include <QTimer>
#include <QDebug>
#include <QDateTime>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    QTimer *t1 = new QTimer(this);
    t1->start(1000);
    connect(t1, &QTimer::timeout, [](){
//        qDebug() << "234";
    });

    QTimer *t2 = new QTimer(this);
    connect(t2, &QTimer::timeout, [this](){
        static int i = 0;
        i++;
        ui->lcdNumber->display(i);
    });

    connect(ui->startBtn, &QPushButton::clicked, [t2](){
        t2->start(1000);
    });

    connect(ui->stopBtn, &QPushButton::clicked, [t2](){
        t2->stop();
    });


    // 倒计时同意
    ui->agreeBtn->setEnabled(false);
    QTimer *t3 = new QTimer(this);
    t3->start(1000);
    connect(t3, &QTimer::timeout, [this, t3](){
        this->t--;
        if(this->t == 0)
        {
            ui->agreeBtn->setEnabled(true);
            ui->agreeBtn->setText("同意");
            t3->stop();
        }
        else
        {
            ui->agreeBtn->setText(QString("同意(%1)").arg(this->t));
        }
    });

    // 放学倒计时
    // 时间戳是 1970.1.1 00:00:00 到任意时刻的秒数 Unix纪元
    // 获取任意时间点的时间戳
    QDateTime nowTime = QDateTime::currentDateTime();
    QDateTime endTime = QDateTime(QDate(2024, 9, 11),QTime(20, 0, 0));

    unsigned int seconds = endTime.toTime_t() - nowTime.toTime_t();

    int h = seconds / 3600;
    int m = seconds % 3600 / 60;
    int s = seconds % 60;
    ui->timeEdit->setTime(QTime(h, m, s));

    QTimer *t4 = new QTimer(this);
    t4->start(1000);
    connect(t4, &QTimer::timeout, [this, endTime, t4](){
        QDateTime nowTime = QDateTime::currentDateTime();
        unsigned int seconds = endTime.toTime_t() - nowTime.toTime_t();
        int h = seconds / 3600;
        int m = seconds % 3600 / 60;
        int s = seconds % 60;
        ui->timeEdit->setTime(QTime(h, m, s));

        if(seconds == 0)
        {
            t4->stop();
        }
    });

}

Widget::~Widget()
{
    delete ui;
}
